﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//Thêm thư viện
using KaraokeManagementDTO;
using System.Data.OleDb;
using System.Data;

namespace KaraokeManagementDAO
{
    public class ChiTietPhieuDatPhongDAO:DataProvider
    {
        public Boolean themChiTiet(ChiTietPhieuDatPhongDTO dto)
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van them chi tiet
            String query = "insert into ChiTietPhieuDatPhong(HangHoa_ID,SoLuongDung,DonGiaBan,GiamGia,PhieuTiepNhanDatPhong_ID) values(@HangHoa_ID,@SoLuongDung,@DonGiaBan,@GiamGia,@PhieuTiepNhanDatPhong_ID)";

            para.Add(new OleDbParameter("@HangHoa_ID", dto.HangHoa_ID));
            para.Add(new OleDbParameter("@SoLuongDung", dto.SoLuongDung));
            para.Add(new OleDbParameter("@DonGiaBan", dto.DonGiaBan));
            para.Add(new OleDbParameter("@GiamGia", dto.GiamGia));
            para.Add(new OleDbParameter("@PhieuTiepNhanDatPhong_ID", dto.PhieuTiepNhanDatPhong_ID));
            
            if (ExecuteNoneQuery(query, para) < 0)
            {
                return false;
            }

            return true;
        }

        public Boolean xoaChiTiet(int MaChiTiet)
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van xoa chi tiet
            String query = "delete from ChiTietPhieuDatPhong where ID=@MaChiTiet";

            para.Add(new OleDbParameter("ID", MaChiTiet));

            if (ExecuteNoneQuery(query, para) < 0)
            {
                return false;
            }

            return true;
        }

        public Boolean capNhatChiTiet(ChiTietPhieuDatPhongDTO dto)
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van them chi tiet
            String query = "update ChiTietPhieuDatPhong set HangHoa_ID=@HangHoa_ID,SoLuongDung=@SoLuongDung,DonGiaBan=@DonGiaBan,GiamGia=@GiamGia,PhieuTiepNhanDatPhong_ID=@PhieuTiepNhanDatPhong where ID=@ID";

            para.Add(new OleDbParameter("@HangHoa_ID", dto.HangHoa_ID));
            para.Add(new OleDbParameter("@SoLuongDung", dto.SoLuongDung));
            para.Add(new OleDbParameter("@DonGiaBan", dto.DonGiaBan));
            para.Add(new OleDbParameter("@GiamGia", dto.GiamGia));
            //para.Add(new OleDbParameter("@GhiChu", dto.GhiChu));
            para.Add(new OleDbParameter("@PhieuTiepNhanDatPhong_ID", dto.PhieuTiepNhanDatPhong_ID));
            //para.Add(new OleDbParameter("@SoLuongKhach", dto.SoLuongKhach));
            para.Add(new OleDbParameter("@ID", dto.ID));

            if (ExecuteNoneQuery(query, para) < 0)
            {
                return false;
            }

            return true;
        }

        public ChiTietPhieuDatPhongDTO layThongTinChiTiet(int MaChiTiet)
        {
            ChiTietPhieuDatPhongDTO chitiet = new ChiTietPhieuDatPhongDTO();
            List<OleDbParameter> para = new List<OleDbParameter>();
 
            //Cau truy van lay thong tin chi tiet
            String query = "select * from ChiTietPhieuDatPhong where ID=@MaChiTiet";

            para.Add(new OleDbParameter("@ID", MaChiTiet));

            //DataTable
            DataTable dt = new DataTable();
            dt = ExecuteQuery(query, para);

            if (dt.Rows.Count == 0)
            {
                return null;
            }

            DataRow row = dt.Rows[0];

            chitiet.ID = int.Parse(row["ID"].ToString());
            chitiet.HangHoa_ID = int.Parse(row["HangHoa_ID"].ToString());
            chitiet.SoLuongDung = int.Parse(row["SoLuongDung"].ToString());
            chitiet.DonGiaBan = Double.Parse(row["DonGiaBan"].ToString());
            chitiet.GhiChu = row["GhiChu"].ToString();
            chitiet.PhieuTiepNhanDatPhong_ID = int.Parse(row["PhieuTiepNhanDatPhong_ID"].ToString());
            //chitiet.SoLuongKhach = int.Parse(row["SoLuongKhach"].ToString());

            return chitiet;
        }

        public List<ChiTietPhieuDatPhongDTO> layDanhSachChiTiet(int MaPhieuDatPhong)
        {
            List<ChiTietPhieuDatPhongDTO> listChiTiet = new List<ChiTietPhieuDatPhongDTO>();

            List<OleDbParameter> para = new List<OleDbParameter>();

            //Câu truy vấn lấy dữ liệu
            String query = "select * from ChiTietPhieuDatPhong where PhieuTiepNhanDatPhong_ID=@MaPhieuDatPhong";


            para.Add(new OleDbParameter("@MaPhieuDatPhong",MaPhieuDatPhong));

            //DataTable
            DataTable dt = new DataTable();
            dt = ExecuteQuery(query, para);

            if (dt.Rows.Count == 0)
            {
                return null;
            }

            foreach (DataRow row in dt.Rows)
            {
                ChiTietPhieuDatPhongDTO dto = new ChiTietPhieuDatPhongDTO();

                dto.ID = int.Parse(row["ID"].ToString());
                dto.HangHoa_ID = int.Parse(row["HangHoa_ID"].ToString());
                dto.SoLuongDung = int.Parse(row["SoLuongDung"].ToString());
                dto.DonGiaBan = Double.Parse(row["DonGiaBan"].ToString());
                dto.GhiChu = row["GhiChu"].ToString();
                dto.PhieuTiepNhanDatPhong_ID = int.Parse(row["PhieuTiepNhanDatPhong_ID"].ToString());
                listChiTiet.Add(dto);
            }

            return listChiTiet;
        }
    }
}
